//
//  TableViewCell.m
//  CivilianTravel
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "TableViewCell.h"
#import "SpotModel.h"

#import "PlayViewController.h"
#import "CustomCollectionViewCell.h"
#import "CityModel.h"
#import "ShoppingViewController.h"
#import "HomeFoodModel.h"

@interface TableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic,strong)NSMutableArray *cityArray;  // 存放所有的城市
@property(nonatomic,strong)NSMutableArray *spoArray;
@property(nonatomic,strong)NSArray  *arrayimage;//  暂存图片
@end

@interface UICollectionViewCell ()



@end


@implementation TableViewCell
-(NSMutableArray *)spoArray{
    if (!_spoArray) {
        _spoArray = [NSMutableArray array];
    }
    return _spoArray;
}

-(NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}


- (void)awakeFromNib {
   
    // 设置collection的相关属性
 
    [self.Collection registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
      layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 2;
    layout.minimumLineSpacing = 2;
    layout.itemSize = CGSizeMake(100, 100);
    self.Collection.backgroundColor = [UIColor whiteColor];
    
    layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
    
    UIImageView *imageView  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"travel.jpg"]];
    imageView.alpha = 0.4;
    [self.Collection addSubview:imageView];


    self.Collection.collectionViewLayout = layout;
   
    self.Collection.dataSource = self;
    self.Collection.delegate = self;
    
    self.Collection.showsHorizontalScrollIndicator = NO;
    self.Collection.bounces = NO;
  
  
    }



-(void)setCellWithModol:(CityModel *)modol{
    
        [self requestSpotWithCityId:modol.ID];
        
    
    
    
}

// 景点请求
-(void)requestSpotWithCityId:(NSNumber *)cityId{
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/poi/search" parameter:@{@"a_t":@"1468410964000",@"radius":@0,@"eachpage":@20,@"startpos":@0,@"cityid":cityId,@"token":@"578624ce19a3f"} method:@"POST" compeleted:^(NSData *data) {
        self.cityArray = [NSMutableArray array];
        NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       
        for (NSDictionary *dic in BigDic[@"data"]) {
            SpotModel *desModol = [[SpotModel alloc]init];
            [desModol setValuesForKeysWithDictionary:dic];
            [self.cityArray addObject:desModol];
        }
        
    } updateUI:^{
        NSMutableArray *array = [NSMutableArray array];
        for (SpotModel *model in self.spoArray) {
            [array addObject:model.avatar];
        }
        self.arrayimage = array.copy;
        
        [self.Collection reloadData];
    }];
}







//  实现方法



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.cityArray.count;
  

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    SpotModel *modol = self.cityArray[indexPath.row];
    
    cell.titleName.text = modol.name;
    
    [cell.Cityimage sd_setImageWithURL:[NSURL URLWithString:modol.avatar]];

    
        return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    UITabBarController *rootvc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    
    NSInteger selectIndex = rootvc.selectedIndex;
    UINavigationController *vc = rootvc.viewControllers[selectIndex];
    
 
    ShoppingViewController *playVC = [[ShoppingViewController alloc]init];
    

    SpotModel * modol = self.cityArray[indexPath.row];
    

    
    playVC.VCid = modol.code;
  
    
    [vc pushViewController:playVC animated:YES];

    
    
}

//是否可以背选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


@end
